﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Chat
{
    class ChatMain
    {
        formChat mainForm;
        Server myServer;
        Client myClient;

        delegate void AddChatTextDelegate(string message);
        AddChatTextDelegate addChatText;

        delegate void AddServerTextDelegate(string message);
        AddServerTextDelegate addServerText;

        public formChat MainForm
        {
            get { return mainForm; }
            set { mainForm = value; }
        }

        public ChatMain()
        {
            mainForm = new formChat();
            mainForm.StartServerButtonClicked += new EventHandler(mainForm_StartServerButtonClicked);
            mainForm.StopServerButtonClicked += new EventHandler(mainForm_StopServerButtonClicked);

            mainForm.ConnectClientButtonClicked += new EventHandler(mainForm_ConnectClientButtonClicked);
            mainForm.DisconnectClientButtonClicked += new EventHandler(mainForm_DisconnectClientButtonClicked);

            mainForm.SendButtonClicked += new EventHandler(mainForm_SendButtonClicked);

            addChatText = mainForm.AddChatText;

        }

        private void InitLocalClient()
        {
            myClient = new Client();
            myClient.ClientMessage += new EventHandler(myClient_MsgReceived);

        }


        void myServer_ServerMessage(object sender, EventArgs e)
        {
            ChatEventArgs tempE = (ChatEventArgs)e;
            //Console.Out.WriteLine("Server msg received: [" + tempE.Data + "]");
            object[] parameters = new object[1];
            parameters[0] = tempE.Message.Text;
            mainForm.Invoke(addServerText, parameters);

        }

        void myClient_MsgReceived(object sender, EventArgs e)
        {
            ChatEventArgs tempE = (ChatEventArgs)e;
            //Console.Out.WriteLine("Client msg received: [" + tempE.Data + "]");
            object[] parameters = new object[1];
            parameters[0] = tempE.Message.Text;
            mainForm.Invoke(addChatText, parameters);

        }

        void mainForm_SendButtonClicked(object sender, EventArgs e)
        {
            myClient.SendMessage(new ChatMessage(mainForm.SendText));

        }

        void mainForm_DisconnectClientButtonClicked(object sender, EventArgs e)
        {

            myClient.Disconnect();
            mainForm.ConnectButton.Enabled = true;
            mainForm.DisconnectButton.Enabled = false;
            mainForm.ClientName.Enabled = true;
        }

        void mainForm_ConnectClientButtonClicked(object sender, EventArgs e)
        {
            InitLocalClient();
            mainForm.ConnectButton.Enabled = false;
            mainForm.DisconnectButton.Enabled = true;
            myClient.ClientName = mainForm.ClientName.Text;
            mainForm.ClientName.Enabled = false;
            myClient.ConnectToServer(mainForm.DestIp, mainForm.DestPort);
        }

        void mainForm_StopServerButtonClicked(object sender, EventArgs e)
        {
            mainForm.StopButton.Enabled = false;
            mainForm.StartButton.Enabled = true;
            myServer.Dispose();
        }

        void mainForm_StartServerButtonClicked(object sender, EventArgs e)
        {
            mainForm.StartButton.Enabled = false;
            mainForm.StopButton.Enabled = true;
            myServer = new Server();

            //setup delegate for adding text to the server textbox
            addServerText = mainForm.AddServerText;


            myServer.ServerMessage += new EventHandler(myServer_ServerMessage);
            myServer.StartServer(mainForm.ServerPort);
            mainForm.AddServerText("Server listening on port " + mainForm.ServerPort);
        }


    }
}
